package com.weixing.mall.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.utils.EnumUtil;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.provider.api.model.enums.AgentLevelEnum;
import com.weixing.mall.provider.api.model.enums.ProfitFromEnum;
import com.weixing.mall.provider.mapper.AgentRebateMapper;
import com.weixing.mall.provider.mapper.AgentWithdrawMapper;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentRebate;
import com.weixing.mall.provider.model.domain.AgentWithdraw;
import com.weixing.mall.provider.model.query.AgentRebateQuery;
import com.weixing.mall.provider.model.vo.AgentRebateVo;
import com.weixing.mall.provider.model.vo.AgentRebateWithdrawVo;
import com.weixing.mall.provider.service.IAgentRebateService;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.util.number.NumberPatternEnum;
import com.weixing.mall.util.number.NumberToRmbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 代理商模块-代理商返利表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentRebateServiceImpl extends ServiceImpl<AgentRebateMapper, AgentRebate> implements IAgentRebateService {

    @Autowired
    private AgentWithdrawMapper agentWithdrawMapper;
    @Autowired
    private IAgentService agentService;
    @Override
    public IPage<AgentRebateVo> selectListOfPage(Page page, AgentRebateQuery agentRebateQuery) {
        IPage<AgentRebateVo> iPage = baseMapper.selectListOfPage(page, agentRebateQuery);
        List<AgentRebateVo> records = iPage.getRecords();
        if (CollUtil.isNotEmpty(records)){
            for (AgentRebateVo vo : records){
                vo.setFmtOrderPrice(NumberToRmbUtil.format(vo.getOrderPrice(), NumberPatternEnum.PAT_DEFAULT.getValue()));
                vo.setFmtRebateAmount(NumberToRmbUtil.format(vo.getRebateAmount(), NumberPatternEnum.PAT_DEFAULT.getValue()));
            }
        }
        return iPage;
    }

    @Override
    public AgentRebateVo rebateDetail(long id) {
        AgentRebate agentRebate = getById(id);
        Agent agent = agentService.getById(agentRebate.getAgentId());
        AgentRebateVo rebateVo = new AgentRebateVo();
        rebateVo.setId(agentRebate.getId());
        rebateVo.setAgentId(agentRebate.getAgentId());
        rebateVo.setAgentLevel(EnumUtil.getIEnum(AgentLevelEnum.class,agent.getAgentLevel()));
        rebateVo.setAgentName(agent.getAgentName());
        rebateVo.setRebateAmount(agentRebate.getRebateAmount());
        rebateVo.setCustomerName(agentRebate.getKefu());
        rebateVo.setIsEnable(EnumUtil.getIEnum(IsEnableEnum.class,agentRebate.getIsEnable()));
        rebateVo.setRebateFrom(EnumUtil.getIEnum(ProfitFromEnum.class,agentRebate.getRebateFrom()));
        rebateVo.setMobile(agent.getMobile());
        rebateVo.setOrderId(agentRebate.getOrderId());
        rebateVo.setOrderPrice(agentRebate.getOrderPrice());
        rebateVo.setOrderSn(agentRebate.getOrderSn());
        rebateVo.setOrderGmtTime(agentRebate.getOrderGmtTime());
        rebateVo.setOrderAuditTime(agentRebate.getOrderAuditTime());
        rebateVo.setFmtOrderPrice(NumberToRmbUtil.format(agentRebate.getOrderPrice(), NumberPatternEnum.PAT_DEFAULT.getValue()));
        rebateVo.setFmtRebateAmount(NumberToRmbUtil.format(agentRebate.getRebateAmount(), NumberPatternEnum.PAT_DEFAULT.getValue()));
        return rebateVo;
    }

    @Override
    public IPage<AgentRebateWithdrawVo> getAgentRebateByWithdrawId(Page page,long id) {
        AgentWithdraw agentWithdraw = agentWithdrawMapper.selectById(id);
        return baseMapper.getAgentRebateByWithdrawId(page,id,agentWithdraw.getCreatedTime());
    }

    @Override
    public IPage<AgentRebateVo> selctAgentRebateOfPage(Page initMpPage, Long agentId) {
        return baseMapper.selctAgentRebateOfPage(initMpPage,agentId);
    }

    @Override
    public BigDecimal countRebate(long agentId, String month) {
        return baseMapper.countRebate(agentId,month);
    }

    @Override
    public AgentRebate findByOrderIdAndAgentId(Long orderId, Long agentId) {
        QueryWrapper<AgentRebate> wrapper = new QueryWrapper<AgentRebate>();
        wrapper.eq("order_id",orderId).eq("agent_id",agentId);
        return getOne(wrapper);
    }
}
